package cn.lg.soar.system.biz.modules.general.client;

import cn.lg.soar.core.annotations.ApiClient;
import cn.lg.soar.system.api.constant.ModuleConst;
import cn.lg.soar.system.api.enums.AccessModeEnum;
import cn.lg.soar.system.api.model.BizFileDTO;
import cn.lg.soar.system.api.model.FastUploadVO;
import cn.lg.soar.system.api.model.FileVO;
import cn.lg.soar.system.api.service.IFileApi;
import cn.lg.soar.system.biz.modules.general.entity.FileMetadata;
import cn.lg.soar.system.biz.modules.general.service.FileService;
import cn.lg.soar.system.biz.modules.general.service.IBizFileService;
import io.swagger.v3.oas.annotations.Hidden;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @author luguoxiang
 * @date 2022/1/16
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Hidden
@ApiClient("boot")
@RestController
@RequestMapping(ModuleConst.CLIENT_PATH + "/file")
public class FileApiClient implements IFileApi {

    @Autowired
    private FileService uploadService;
    @Autowired
    private IBizFileService bizFileRelationService;

    @PostMapping("fastUpload")
    @Override
    public FileVO fastUpload(FastUploadVO vo) {
        FileMetadata metadata = uploadService.fastUpload(vo);
        return uploadService.toVo(metadata, vo.getAccessMode());
    }

    @PostMapping("upload-dir")
    @Override
    public FileVO upload(
            @RequestPart("file") MultipartFile file,
            @RequestParam("directory") String directory,
            @RequestParam("accessMode") AccessModeEnum accessMode
    ) {
        FileMetadata metadata = uploadService.upload(file, directory);
        return uploadService.toVo(metadata, accessMode);
    }

    @PostMapping("upload")
    @Override
    public FileVO upload(@RequestPart("file") MultipartFile file, @RequestParam("accessMode") AccessModeEnum accessMode) {
        FileMetadata metadata = uploadService.upload(file);
        return uploadService.toVo(metadata, accessMode);
    }

    @PostMapping("saveBizFile")
    @Override
    public boolean saveBizFile(@RequestParam("bizId") Long bizId, @RequestBody Collection<BizFileDTO> bizFiles) {
        return bizFileRelationService.saveBizFile(bizId, bizFiles);
    }

    @GetMapping("getBizFile")
    @Override
    public Map<String, List<FileVO>> getBizFile(@RequestParam("bizId") Long bizId, @RequestParam("bizTypes") Collection<String> bizTypes) {
        return bizFileRelationService.getBizFile(bizId, bizTypes);
    }

    @PostMapping("removeBizFile")
    @Override
    public boolean removeBizFile(@RequestParam("bizId") Long bizId, @RequestParam("bizTypes") Collection<String> bizTypes) {
        return bizFileRelationService.removeBizFile(bizId, bizTypes);
    }

}
